package com.soledroid;

import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.soledroid.Model.Canteen;
import com.soledroid.Model.Dish;
import com.soledroid.Model.Stall;
import com.soledroid.Model.User;
import com.soledroid.Model.Checkin;
import com.soledroid.Model.Comment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class RegisterActivity extends Activity{
	
	protected EditText usernameTextField;
	protected EditText fullnameTextField;
	protected String android_id;
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        
        android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
        
        TextView udidView = (TextView) findViewById(R.id.udidTextView);
        usernameTextField = (EditText) findViewById(R.id.usernameTextField);
        fullnameTextField = (EditText) findViewById(R.id.registerFullnameTextField);
        
        udidView.setText("UDID: " + android_id);
        
        Button signupBtn = (Button) findViewById(R.id.signupBtn);
        signupBtn.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		((Button) v).setText("Submiting..");
        		HttpClient httpclient = new DefaultHttpClient();
        	    HttpPost httppost = new HttpPost("http://soledroid.huytoan.com/user");

        	    try {
        	        // Add your data
        	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        	        nameValuePairs.add(new BasicNameValuePair("username", usernameTextField.getText().toString()));
        	        nameValuePairs.add(new BasicNameValuePair("fullname", fullnameTextField.getText().toString()));
        	        nameValuePairs.add(new BasicNameValuePair("udid", android_id));
        	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        	        // Execute HTTP Post Request
        	        HttpResponse response = httpclient.execute(httppost);
        	        
        	        Toast.makeText(getApplicationContext(), "Account created!", Toast.LENGTH_LONG).show();
        	        // back to dashboard activity
        	        finish();
        	    } catch (Exception e) {
        	        e.printStackTrace();
        	    }
        	}
        });
	}

}
